<?php
/**
 * 后台文章模型
 * @author cjli
 *
 */
class ArticleModel extends Model
{
	/**
	 * 获取文章信息
	 * @param int $aid 文章ID
	 */
	public static function getArticleInfo($aid)
	{
		$article_db = M('article');
		$articleInfo = $article_db->find($aid);
		return $articleInfo;
	}
	
	/**
	 * 获取文章内容详细
	 * @param int $aid 文章ID
	 * @param int $language_id 语言ID号
	 */
	public static function getArticleItems($aid, $language_id = 0)
	{
		$where['article_id'] = $aid;
		if($language_id){
			$where['language_id'] = $language_id;
		}
		$articleItemsArray = array();
		$article_items_db = D('article_items');
		$articleItems = $article_items_db->where($where)->select();
		if($articleItems){
			foreach ($articleItems as $item){
				$articleItemsArray[$item['language_id']] = $item;
			}
		}
		return $articleItemsArray;
	}
	
	/**
	 * 更新文章信息
	 * @param array $post post数据
	 * @param int $aid 文章ID
	 */
	public static function updateArticleInfo($post, $aid = null)
	{
		$article_db = M('article');
		$data = array(
			'catid' => intval($post['catid']),
			'visit' => intval($post['visit']),
			'img' => trim($post['img']),
			'path_alias' => trim($post['path_alias']),
			'template' => trim($post['template']),
			'end_time' => time(),
			'listorder' => intval($post['listorder']),
			'display' => intval($post['display']),
			'article_url' => trim($post['article_url']),
		);
		if($aid){
			$article_db->where(array('id' => $aid))->save($data);
		}else{
			$data['start_time'] = time();
			$aid = $article_db->add($data);
		}
		self::updateArticleCache($aid);
		return $aid;
	}
	
	/**
	 * 更新文章内容信息
	 * @param array $post post数据
	 * @param int $aid 文章ID
	 */
	public static function updateArticleItems($post, $aid)
	{
		$articleInfo = self::getArticleInfo($aid);
		if(!$articleInfo) return false;
		
		$article_items_db = D('article_items');
		$article_items_db->where(array('article_id' => $aid))->delete();
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			if($post[$language_id]['name']){
				//自动提取摘要
				if(empty($post[$language_id]['summary']) && $post[$language_id]['add_introduce'] && $post[$language_id]['description']){
					import('ORG.Util.Input');
					$content = Input::deleteHtmlTags($post[$language_id]['description']);
					$introcude_length = intval($post[$language_id]['introcude_length']);
					$post[$language_id]['summary'] = strwidth($content, $introcude_length);
				}
				
				$data = array(
					'article_id' => $aid,
					'language_id' => $language_id,
					'name' => trim($post[$language_id]['name']),
					'author' => trim($post[$language_id]['author']),
					'source' => trim($post[$language_id]['source']),
					'summary' => trim($post[$language_id]['summary']),
					'description' => $post[$language_id]['description'],
					'meta_title' => trim($post[$language_id]['meta_title']),
					'meta_keywords' => trim($post[$language_id]['meta_keywords']),
					'meta_description' => trim($post[$language_id]['meta_description']),
				);
				$article_items_db->add($data);
			}
		}
	}
	
	/**
	 * 删除文章内容
	 * @param int $aid 文章ID
	 */
	public static function deleteArticle($aid)
	{
		$article_db = M('article');
		$article_db->where(array('id' => $aid))->delete();
		$article_items_db = D('article_items');
		$article_items_db->where(array('article_id' => $aid))->delete();
		self::updateArticleCache($aid);
	}
	
	/**
	 * 更新文章内容缓存
	 * @param int $aid 文章ID
	 */
	public static function updateArticleCache($aid)
	{
		if(!intval($aid)) return false;
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			S('article_'.$aid.'_'.$language_id, NULL);
		}
	}
	
	/**
	 * 更新单页信息
	 * @param array $post post数据
	 * @param int $pid 单页ID
	 */
	public static function updatePageInfo($post, $pid = null)
	{
		$page_db = M('page');
		$data = array(
			'path_alias' => trim($post['path_alias']),
			'template' => trim($post['template']),
			'start_time' => time(),
			'listorder' => intval($post['listorder']),
			'display' => intval($post['display']),
		);
		if($pid){
			$page_db->where(array('id' => $aid))->save($data);
		}else{
//			$data['start_time'] = time();
			$pid = $page_db->add($data);
		}
		return $pid;
	}
	
	/**
	 * 更新单页内容信息
	 * @param array $post post数据
	 * @param int $pid 单页ID
	 */
	public static function updatePageItems($post, $pid)
	{
		if(!intval($pid)) return false;
		$page_items_db = D('page_items');
		$page_items_db->where(array('page_id' => $pid))->delete();
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			if($post[$language_id]['name']){
				$data = array(
					'page_id' => $pid,
					'language_id' => $language_id,
					'name' => trim($post[$language_id]['name']),
					'summary' => trim($post[$language_id]['summary']),
					'description' => $post[$language_id]['description'],
					'meta_title' => trim($post[$language_id]['meta_title']),
					'meta_keywords' => trim($post[$language_id]['meta_keywords']),
					'meta_description' => trim($post[$language_id]['meta_description']),
				);
				$page_items_db->add($data);
			}
		}
		self::updatePageCache($pid);
	}
	
	/**
	 * 删除单个页面
	 * @param int $pid 单页ID
	 */
	public static function deletePageInfo($pid)
	{
		$page_db = D('page');
		$page_db->delete($pid);
		$page_items_db = D('page_items');
		$page_items_db->where(array('page_id' => $pid))->delete();
		ArticleModel::updatePageCache($pid);
	}
	
	/**
	 * 获取文章信息
	 * @param int $aid 文章ID
	 */
	public static function getPageInfo($pid)
	{
		$page_db = M('page');
		$pageInfo = $page_db->find($pid);
		return $pageInfo;
	}
	
	/**
	 * 获取单页内容详细
	 * @param int $pid 单页ID
	 */
	public static function getPageItems($pid)
	{
		$pageItemsArray = array();
		$page_items_db = D('page_items');
		$pageItems = $page_items_db->where(array('page_id' => $pid))->select();
		if($pageItems){
			foreach ($pageItems as $page){
				$pageItemsArray[$page['language_id']] = $page;
			}
		}
		return $pageItemsArray;
	}
	
	/**
	 * 更新单页缓存
	 * @param int $pid 单页ID
	 */
	public static function updatePageCache($pid)
	{
		if(!intval($pid)) return false;
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			S('article_page_list_'.$language_id, NULL);
		}
	}
}